<template>
  <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="8">
          <el-form-item label="报关单号" prop="customs_number">
            <el-input v-model="form.customs_number" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="上传文件">
        <el-upload
          ref="upload"
          class="upload-demo"
          :headers="importHeaders"
          :action="url.import"
          :on-success="handleSuccess"
          :on-change="handleImport"
          :file-list="fileList"
          :multiple="false"
          :limit="1"
          accept=".csv,.pdf,.PDF, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
        >
          <el-button size="mini" type="success">选择文件</el-button>
          <div slot="tip" class="el-upload__tip">只能上传csv/xlsx/xls/pdf文件</div>
        </el-upload>
      </el-form-item>
      <!--      <el-row>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="申报日期" prop="declare_date">-->
      <!--            <el-date-picker v-model="form.declare_date" value-format="yyyy-MM-dd" type="date" placeholder="委托日期" style="width: 100%;" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="进/出" prop="enter_out">-->
      <!--            <el-select v-model="form.enter_out" placeholder="进/出" clearable style="width: 100%">-->
      <!--              <el-option label="进口" value="1" />-->
      <!--              <el-option label="出口" value="2" />-->
      <!--            </el-select>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="进/出口日期" prop="enter_out_date">-->
      <!--            <el-date-picker v-model="form.enter_out_date" value-format="yyyy-MM-dd" type="date" placeholder="进/出口日期" style="width: 100%;" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->

      <!--      <el-row>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="关区代码" prop="customs_district">-->
      <!--            <el-input v-model="form.customs_district" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="报关单号" prop="declare_number">-->
      <!--            <el-input v-model="form.declare_number" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="联单数" prop="single_copy_num">-->
      <!--            <el-input v-model="form.single_copy_num" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->

      <!--      <el-row>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="品名数" prop="product_num">-->
      <!--            <el-input v-model="form.product_num" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="贸易方式" prop="trade_mode">-->
      <!--            <el-select v-model="form.trade_mode" placeholder="贸易方式" clearable style="width: 100%">-->
      <!--              <el-option label="一般贸易" value="1" />-->
      <!--            </el-select>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="经营单位" prop="business_unit">-->
      <!--            <el-input v-model="form.business_unit" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->

      <!--      <el-row>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="报关件数" prop="declare_piece">-->
      <!--            <el-input v-model="form.declare_piece" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="报关毛重" prop="declare_gross_weight">-->
      <!--            <el-input v-model="form.declare_gross_weight" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="金额描述" prop="money_describe">-->
      <!--            <el-input v-model="form.money_describe" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->

      <!--      <el-row>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="合计USD" prop="totalUSD">-->
      <!--            <el-input v-model="form.totalUSD" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="进出口岸" prop="enter_out_port">-->
      <!--            <el-input v-model="form.enter_out_port" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="备注" prop="remarks">-->
      <!--            <el-input v-model="form.remarks" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->

      <!--      <el-row>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="退回日期" prop="return_date">-->
      <!--            <el-date-picker v-model="form.return_date" value-format="yyyy-MM-dd" type="date" placeholder="退回日期" style="width: 100%;" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="退还客户日期" prop="deterioration_client_date">-->
      <!--            <el-date-picker v-model="form.deterioration_client_date" value-format="yyyy-MM-dd" type="date" placeholder="退还客户日期" style="width: 100%;" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="输机员" prop="transport_crew">-->
      <!--            <el-input v-model="form.transport_crew" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->

      <!--      <el-row>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="审单员" prop="document_reviewer">-->
      <!--            <el-input v-model="form.document_reviewer" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="18位海关编码" prop="customs_code">-->
      <!--            <el-input v-model="form.customs_code" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="清单编码" prop="detailed_code">-->
      <!--            <el-input v-model="form.detailed_code" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->

      <!--      <el-row>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="企业外汇类型" prop="enterprise_remit_type">-->
      <!--            <el-input v-model="form.enterprise_remit_type" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="8">-->
      <!--          <el-form-item label="报关单状态" prop="declare_single_status">-->
      <!--            <el-input v-model="form.declare_single_status" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->

      <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
        <el-col :span="24">
          <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
          <el-button size="medium" @click="close()">取消</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  name: 'Declaration',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    },
    tableData: {
      type: Array
    }
  },
  data() {
    return {
      rules: {
        enter_out: [
          { required: true, message: '请选择进/出口', trigger: 'change' }
        ]
      },
      url: {
        import: process.env.VUE_APP_BASE_API + '/upload/file'
      },
      fileList: [],
      importHeaders: { authorization: 'Bearer ' + this.token }
    }
  },
  computed: {
    ...mapGetters([
      'token'
    ])
  },
  methods: {
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      console.log(this.tableData.length)
      this.form.key = this.tableData.length + 1
      this.tableData.push(this.form)
      this.close()
    },
    edit() {
      this.close()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    },
    // 导入成功提示
    handleSuccess(response, file, fileList) {
      console.log(response)
      if (response.code === 10000) {
        this.form.file_path = response.data
        this.$message.success(response.message)
      } else {
        this.$message.error(response.message)
      }
    },
    // 打开导入弹窗
    handleImport() {
      this.importHeaders.authorization = 'Bearer ' + this.token
      this.url.import = process.env.VUE_APP_BASE_API + '/account/importDeclaration'
    }
  }
}
</script>
